﻿using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using APMS.WebSite.Common;
using APMS.WebSite.Models.DataStore;
using APMS.WebSite.Models.ViewData.Pages;

namespace APMS.WebSite.Controllers
{
    /// <summary>
    /// WriteboardController controls all Views related to Writeboard operation which are:
    /// * Index (/InProject/{projectId}/Writeboard/)
    /// * List (/InProject/{projectId}/Writeboard/List)
    /// * Create (/InProject/{projectId}/Writeboard/Create)
    /// * Update (/InProject/{projectId}/Writeboard/Update/{id})
    /// * View (/InProject/{projectId}/Writeboard/View/{id})
    /// * Delete (/InProject/{projectId}/Writeboard/Delete/{id})
    /// * MORE
    /// </summary>
    /// <version>$Revision$ $Date$</version>
    /// <history>
    /// <record>09/15/2008. raQuiam - Created this file</record>
    /// </history>
    [HandleError]
    public class WriteboardController : ProjectBaseController
    {
        #region Keys for ViewData
        /// <summary>
        /// Use in method List
        /// </summary>
        public const string ALL_WRITEBOARDS = "AllWriteboard";
        #endregion

        #region List Action
        /// <summary>
        /// List all Todo under target project 
        /// </summary>
        /// <returns></returns>
        public ActionResult List(int projectId)
        {
            // Initialize data required by master page
            ActionResult exception = base.InitializeAction(MainNavItemType.Writeboard);
            if (exception != null)
                return exception;

            // Get current project
            Project currentProject = base.GetProject(projectId);

            // Set Window Title
            base.SetTitle(currentProject.ProjectName + " | Writeboards", true);
            base.SetInfoToUserControls(currentProject);

            // TODO: Query

            MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();
            var query =
                from writeBoard in db.WriteBoard
                from writeBoardversion in db.WriteBoardVersion
                where
                    writeBoardversion.WriteBoardID == writeBoard.WriteBoardID &&
                    writeBoard.ProjectID == projectId
                // select max writeBoardversion
                select writeBoardversion;

            ViewData[ALL_WRITEBOARDS] = query.ToList();
            ViewData[CURRENT_PROJECT] = currentProject;

            return View("List");
        }
        #endregion

        #region Create Action
        /// <summary>
        /// Create new Message
        /// </summary>
        /// <returns></returns>
        public ActionResult Create(int projectId)
        {
            return View("Create");
        }
        #endregion

        #region View/Update Actions
        public ActionResult View(int projectId, int id)
        {
            return View("View");
        }

        public ActionResult Update(int projectId, int id)
        {
            return View("Update");
        }
        #endregion

        #region Delete Action
        public ActionResult Delete(int projectId, int id)
        {
            // Redirect to  List Todos
            return RedirectToAction("List", new { projectId = projectId });
        }
        #endregion

        #region From Oat
        //public ActionResult create(int projectId, string writeboard_detail)
        //{
        //    //throw new NotImplementedException();   
        //    String username = "admin";
        //    MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();
        //    WriteBoard newWriteBoard = new WriteBoard();
        //    newWriteBoard.ProjectID = projectId;
        //    newWriteBoard.AddDate = DateTime.Now;

        //    db.WriteBoard.Add(newWriteBoard);
        //    db.SubmitChanges();
        //    //-------------------------------------------------

        //    WriteBoardVersion newWriteBoardVersion = new WriteBoardVersion();
        //    newWriteBoardVersion.WriteBoardID = newWriteBoard.WriteBoardID;
        //    newWriteBoardVersion.UserName = username;
        //    newWriteBoardVersion.WriteBoardDetail = writeboard_detail;
        //    newWriteBoardVersion.ModifyDate = DateTime.Now;
        //    newWriteBoardVersion.AddDate = DateTime.Now;

        //    db.WriteBoardVersion.Add(newWriteBoardVersion);
        //    db.SubmitChanges();

        //    return View("test");
        //}

        //public ActionResult Delete(int projectId, int id)
        //{
        //    //throw new NotImplementedException();
        //    MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();


        //    try
        //    {
        //        WriteBoard deleteWriteBoard = db.WriteBoard.Single(p => p.WriteBoardID == id);
        //        //var deleteWriteBoardVersion = db.WriteBoardVersion.SelectMany(p => p.WriteBoardID == deleteWriteBoard.WriteBoardID);
        //        //delete all version
        //        //db.WriteBoardVersion.RemoveAll(deleteWriteBoardVersion);
        //        db.WriteBoard.Remove(deleteWriteBoard);
        //        db.SubmitChanges();
        //    }
        //    catch (InvalidOperationException ex)
        //    {
        //        System.Console.WriteLine("called on set with zero items");
        //    }
        //    ////////////////////// DELETE DATA

        //    return Index();

        //    return View("test");
        //}


        //public ActionResult Update(int projectId, int id)
        //{
        //    // Initialize data required by master page

        //    ActionResult exception = base.InitializeAction();
        //    if (exception != null)
        //        return exception;
        //    String username = "admin";

        //    // Ser Window Title
        //    base.SetTitle("Writeboard | " + id);

        //    // SELECT * FROM user WHERE user.UserName = '{currentUser}'
        //    MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();
        //    var query =
        //        from writeboardversion in db.WriteBoardVersion
        //        where
        //            writeboardversion.WriteBoardID == id
        //        select writeboardversion;

        //    // Prepare ViewData
        //    ManageWriteboardViewData viewData = new ManageWriteboardViewData();
        //    WriteBoardVersion lastWB = query.First<WriteBoardVersion>();
        //    viewData.CurrentWriteboard = lastWB;
        //    ViewData["Allversion"] = query.ToList();

        //    //TODO: Handle both POST and GET methods
        //    if (Request.HttpMethod == Constants.HttpMethod.POST)
        //    {
        //        // If POST means UPDATE the value from Request.Form to DB
        //        // If GET means VIEW the value from DB
        //        #region Update record back to DB

        //        WriteBoardVersion newWriteBoardVersion = new WriteBoardVersion();
        //        newWriteBoardVersion.WriteBoardID = lastWB.WriteBoardID;
        //        newWriteBoardVersion.UserName = username;
        //        newWriteBoardVersion.WriteBoardDetail = Request.Form["writeboard_detail"];
        //        newWriteBoardVersion.ModifyDate = DateTime.Now;
        //        newWriteBoardVersion.AddDate = DateTime.Now;

        //        viewData.IsCurrentWriteboardUpdate = true;

        //        db.SubmitChanges();
        //        #endregion
        //    }
        //    else
        //    {
        //        viewData.IsCurrentWriteboardUpdate = false;
        //    }

        //    return View("Update", viewData);
        //}


        //public ActionResult List(int projectId)
        //{
        //    MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();
        //    var query =
        //        from writeBoard in db.WriteBoard
        //        from writeBoardversion in db.WriteBoardVersion
        //        where
        //            writeBoardversion.WriteBoardID == writeBoard.WriteBoardID &&
        //            writeBoard.ProjectID == projectId
        //        // select max writeBoardversion
        //        select writeBoardversion;

        //    ViewData["all"] = query.ToList();

        //    return View("test");
        //}
        #endregion
    }
}
